package com.ptbuy.tranrecord.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ptbuy.R;
import com.ptbuy.db.pojo.ClientPojo;
import com.ptbuy.utils.Utils;

import java.text.DecimalFormat;
import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

    Context mContext;
    List<ClientPojo> data;

    public interface ClientInterface {
        void optProject(int type, ClientPojo dataBean, boolean isChecked);
    }

    public ClientInterface projectInterface;

    public void setProjectInterface(ClientInterface projectInterface) {
        this.projectInterface = projectInterface;
    }

    private void optProject(int type, ClientPojo dataBean, boolean isChecked) {
        projectInterface.optProject(type, dataBean, isChecked);
    }

    //有参构造器，给外部传数据进来适配器
    public RecyclerViewAdapter(List<ClientPojo> data, Context context) {
        this.data = data;
        this.mContext = context;
    }

    //创建视图
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tran_card_layout, parent, false);
        return new RecyclerViewHolder(v);
    }

    //赋值操作
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {

        ClientPojo clientPojo = data.get(position);
        holder.itemName.setText(clientPojo.getName());
        holder.itemNameOrder.setText((data.size() - position) + "");
        holder.itemPhone.setText(clientPojo.getPhone() + "");
        holder.itemAgentName.setText(clientPojo.getAgentName());
        holder.itemTotalProduct.setText(String.valueOf(clientPojo.getTotalProduct()));


        String totalPrice = clientPojo.getTotalPrice();
        String numlStr = Utils.getNumlStr(totalPrice);
        holder.itemTotalPrice.setText(numlStr + "元");
        String time = clientPojo.getTime();
        String s = Utils.stringToTime(time);
        holder.itemTime.setText(s);//记录时间

        //删除
        holder.itemRemove.setOnClickListener(v -> {
            optProject(2, clientPojo, false);
        });
        holder.itemTranModify.setOnClickListener(v -> {
            optProject(4, clientPojo, false);
        });

        holder.itemProductView.setVisibility(View.VISIBLE);
        //查看照片视频
        holder.itemProductView.setOnClickListener(v -> {
            optProject(3, clientPojo, false);
        });

        //查看签字确认单
        holder.viewSignSubmit.setOnClickListener(v -> {
            optProject(5, clientPojo, false);
        });

        //导出签字确认单
        holder.exportSignSubmit.setOnClickListener(v -> {
            optProject(6, clientPojo, false);
        });

    }

    //条目的数量
    @Override
    public int getItemCount() {
        return data.size();
    }

    //声明控件，绑定控件
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView itemName;//客户姓名
        TextView itemNameOrder;//客户编号
        TextView itemPhone;//客户电话
        TextView itemAgentName;//收购人
        TextView itemTotalProduct;//商品总件数
        TextView itemTotalPrice;//商品总价
        TextView itemTime;//记录时间
        //动作
        Button itemRemove;//删除
        Button itemProductView;//查看商品
        Button itemTranModify;//查看商品
        Button viewSignSubmit;//查看签字确认单
        Button exportSignSubmit;//导出签字确认单

        public RecyclerViewHolder(View v) {
            super(v);
            itemName = v.findViewById(R.id.itemName);
            itemNameOrder = v.findViewById(R.id.itemNameOrder);
            itemPhone = v.findViewById(R.id.itemPhone);
            itemAgentName = v.findViewById(R.id.itemAgentName);
            itemTotalProduct = v.findViewById(R.id.itemTotalProduct);
            itemTotalPrice = v.findViewById(R.id.itemTotalPrice);
            itemTime = v.findViewById(R.id.itemTime);
            itemRemove = v.findViewById(R.id.itemRemove);
            itemProductView = v.findViewById(R.id.itemProductView);
            itemTranModify = v.findViewById(R.id.itemTranModify);
            viewSignSubmit = v.findViewById(R.id.viewSignSubmit);
            exportSignSubmit = v.findViewById(R.id.exportSignSubmit);
        }
    }
}
